<?php
/**
 * @copyright (c) 2011 jooyea.net
 * @file taobao_csv.php
 * @brief csv文件导入、导出
 * @author relay
 * @date 2011-10-12
 * @version 2.1
 */
namespace Org\Yike;
/**
 * @class ITaobaoCsv
 * @brief taobao_csv文件导入、导出
 */
class Csv
{
    /**
     * @brief 构造函数
     * */
    public function __construct(){}

    /**
     *导入淘宝CSV文件
     * @param <文件路径> $name
     */
    function import($name)
    {
        $data = $this->get_csv_date($name);


        return $data;
    }
    /**
     * 解析csv文件
     */
    function get_csv_date($file_name)
    {
        $str = file_get_contents($file_name);
        $data = array();
        if($str{0} != "\xFF" || $str{1} != "\xFE"){
            return false;
        }

        //转码
        $str = $this->unicodeToUtf8(substr($str, 2));
        //切割字符串
        $str = preg_replace('/\t\"([^\t]+?)\"\t/es', "'\t\"' . stripslashes(str_replace(\"\n\", \"\", '\\1')) . '\"\t'", $str);
        $csv_array = explode("\n", $str);
        unset($csv_array[count($csv_array) -1]);
        unset($csv_array[0]);
        $product_array = array();
        if (!empty($csv_array)){
            foreach ($csv_array as $k => $v){
                $tmp = explode("\t", $v);
                //商品名称
                $data['title'] = str_replace("'",'',str_replace('"','',$tmp[0]));
                //淘宝id
                $data['num_iid'] = str_replace("'",'',str_replace('"','',$tmp[33]));
                //商品价格
                $data['price'] = number_format($tmp[7],2);
                //淘宝类目id
                $data['cid']   = intval($tmp[1]);
                $data['picture'] = str_replace("'",'',str_replace('"','',$tmp[28]));
                $data['subtitle'] = str_replace("'",'',str_replace('"','',$tmp[52]));

                $product_array[] = $data;
            }
        }

        return $product_array;
    }
    function unicodeToUtf8($str,$order="little")
    {
        $utf8string ="";
        $n=strlen($str);
        for ($i=0;$i<$n ;$i++ )
        {
            if ($order=="little")
            {
                $val = str_pad(dechex(ord($str[$i+1])), 2, 0, STR_PAD_LEFT) .
                    str_pad(dechex(ord($str[$i])),      2, 0, STR_PAD_LEFT);
            }
            else
            {
                $val = str_pad(dechex(ord($str[$i])),      2, 0, STR_PAD_LEFT) .
                    str_pad(dechex(ord($str[$i+1])), 2, 0, STR_PAD_LEFT);
            }
            $val = intval($val,16); // 由于上次的.连接，导致$val变为字符串，这里得转回来。
            $i++; // 两个字节表示一个unicode字符。
            $c = "";
            if($val < 0x7F)
            { // 0000-007F
                $c .= chr($val);
            }
            elseif($val < 0x800)
            { // 0080-07F0
                $c .= chr(0xC0 | ($val / 64));
                $c .= chr(0x80 | ($val % 64));
            }
            else
            { // 0800-FFFF
                $c .= chr(0xE0 | (($val / 64) / 64));
                $c .= chr(0x80 | (($val / 64) % 64));
                $c .= chr(0x80 | ($val % 64));
            }
            $utf8string .= $c;
        }
        /* 去除bom标记 才能使内置的iconv函数正确转换 */
        if (ord(substr($utf8string,0,1)) == 0xEF && ord(substr($utf8string,1,2)) == 0xBB && ord(substr($utf8string,2,1)) == 0xBF)
        {
            $utf8string = substr($utf8string,3);
        }
        return $utf8string;
    }

    /**
     *编码转换
     * @param <string> $strInput
     * @return <type>
     */
    function i($strInput)
    {
        return iconv('utf-8','UTF-16LE',$strInput);
    }
}
?>